﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public partial class TQP02 : ViewBase
    {
        #region ==QPLAN===========
        public static readonly DependencyProperty QPLANProperty = DependencyProperty.Register("QPLAN",
            typeof(string), typeof(TQP02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPLANChanged));
        public string QPLAN
        {
            get
            {
                return (string)GetValue(QPLANProperty);
            }
            set
            {
                SetValue(QPLANProperty, value);
                ((VQPPLAN)MV).QPLAN = value;
                if (value.Length == 8)
                {
                    KRS = QPLAN.Substring(0, 2);
                    LFD = QPLAN.Substring(2, 6);
                }
            }
        }
        private static void OnQPLANChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQP02;
            if (src != null)
            {
                src.QPLAN = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwQP===========
        public static readonly DependencyProperty dwQPProperty = DependencyProperty.Register("dwQP",
            typeof(DataRow), typeof(TQP02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwQPChanged));
        public DataRow dwQP
        {
            get
            {
                return (DataRow)GetValue(dwQPProperty);
            }
            set
            {
                SetValue(dwQPProperty, value);
            }
        }
        private static void OdwQPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TQP02;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwQP = row;
            }

        }
        #endregion


        public TQP02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VQPPLAN();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwQP = dwMaster;

            if (dwQP != null)
            {
                QPLAN = dwQP["QPPPNRKRS"].ToString() + dwQP["QPPPLFDNR"].ToString();
            }
            else
            {
                QPLAN = "";
            }
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            //GPPARTIE GPGART GPGFARBE GPBEM1 GPBEM2
            dwData = TBMain.NewRow();
            dwData["QPPMANDANT"] = app.Mandant;
            dwData["QPPARTST"] = ARTST;
            dwData["QPPPNRKRS"] = KRS;
            dwData["QPPPLFDNR"] = LFD;
            dwData["QPPSPEZIF"] = DES;
            if (LFD.Trim() == "000000")
            {
                dwData["QPPPLFDNR"] = "0";
            }
            SResult = ((VQPPLAN)MV).QueryQPPLAN(dwData, 3);
            MergeData(SResult, true);
            base.QueryCommand(sender, e);
        }
        VQPSTEP QS;
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwQP == null)
                return;
            ((VQPPLAN)MV).DeleteQPPLAN(dwQP);
            if (QS == null)
                QS = new VQPSTEP();
            dwData = QS.TBMain.NewRow();
            dwData["QPSMANDANT"] = app.Mandant;
            dwData["QPSPNRKRS"] = dwQP["QPPPNRKRS"].ToString();
            dwData["QPSPLFDNR"] = dwQP["QPPPLFDNR"].ToString();
            QS.DeleteQPSTEP(dwData, 2);
            filter = "QPPPNRKRS='" + dwQP["QPPPNRKRS"].ToString() + "' AND QPPPLFDNR='" + dwQP["QPPPLFDNR"].ToString() + "'";
            rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }
    }
}
